package net.borderwars.simulator.events;

/**
 * @author Eric
 *         Date: Mar 8, 2004
 *         Time: 9:51:00 PM
 */
public class TimeSynchEvent extends BEvent {
    final long realLifePause = 100;
    long simulatorPause;
    final float ratio = 1.0f;

    long total = 0;
    long count = 0;

    long lastMark = System.currentTimeMillis ();

    public TimeSynchEvent (long l) {
        super (l);
        simulatorPause = (long) (realLifePause * ratio);
    }

    public void process () {
        long current = System.currentTimeMillis ();
        long delta = (current - lastMark); // we already taken up much this time
        long moreTimeToWait = realLifePause - delta;
        total += moreTimeToWait;
        count++;
//        if (count % 100 == 0) {
//            System.err.println("Average wasted cycles " + total / count);
//        }
        if (moreTimeToWait > 0) {
//            try {
//                Thread.sleep(moreTimeToWait);
//            } catch (InterruptedException encoder) {
//                encoder.printStackTrace();
//            }
        }
        timeStamp = new Long (this.timeStamp.longValue () + simulatorPause);
        eq.placeEvent (this);
        lastMark = System.currentTimeMillis ();

    }

    public String getName () {
        return ("Time Synch");
    }
}
